// pages/shop/shop.js

import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast
} from "../../utils/asyncWx"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  handleChooseAddress() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address = wx.getStorageSync('address')
    // 获取缓存中购物车的数据
    const cart = wx.getStorageSync('cart') || []
    this.setData({
      address
    })
    this.setCart(cart)
  },

  async handleChooseAddress() {
    try {
      // 会出来一个提示框
      const res1 = await getSetting()
      // 点确定 scopeAddress是true 直接获取收货地址
      // 从未调用过 scopeAddress是undefined 没有获取
      // 点取消 scopeAddress是false
      const scopeAddress = res1.authSetting['scope.address']
      if (scopeAddress === false) {
        await openSetting()
      }
      let address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
      wx.setStorageSync('address', address)
    } catch (error) {
      console.log('1111')
    }
  },
  handleItemChange(e) {
    // 获取被修改的商品的id
    const goods_id = e.currentTarget.dataset.id;
    let {
      cart
    } = this.data
    let index = cart.findIndex(v => v.goods_id === goods_id)
    cart[index].checked = !cart[index].checked;
    this.setCart(cart)
  },

  setCart(cart) {
    let allChecked = true
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
      } else {
        // 没有全部选中
        allChecked = false;
      }
    })
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
      cart,
      totalNum,
      totalPrice,
      allChecked
    })
    wx.setStorageSync('cart', cart)
  },
  handleItemAllCheck() {
    let {
      cart,
      allChecked
    } = this.data;
    allChecked = !allChecked
    cart.forEach(v => {
      v.checked = allChecked
    })
    this.setCart(cart)
  },
  async handleItemNumEdit(e) {
    console.log(e.currentTarget.dataset)
    let { id, operation } = e.currentTarget.dataset;
    let { cart } = this.data
    let index = cart.findIndex(v=>v.goods_id===id)
    // 是否需要删除
    if(cart[index].num===1&&operation===-1) {
      // 删除
      const res = await showModal({
        content:'您是否需要删除'
      })
      console.log(res)
      if(res.confirm) {
        cart.splice(index,1)
        this.setCart(cart)
      }
    }else{ 
      cart[index].num += operation
      this.setCart(cart)
    }
  },
  async handlePay() {
    const { address, totalNum } = this.data
    if(!address.userName) {
      // 没有
      await showToast({
        title: "您还没有选择收货地址"
      })
      return
    }
    if(totalNum===0) {
      // 没有选择商品
      await showToast({
        title: "您还没有选择商品"
      })
      return;
    }
    wx.navigateTo({
      url: "/pages/pay/pay"
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})